#!/bin/bash

# Function to display help message
show_help() {
    echo "Usage: $0 --role <cluster-init|agent> --vip <IPv4 address> --token <string>"
    echo ""
    echo "Options:"
    echo "  --role    Role of the node (cluster-init or agent)"
    echo "  --vip     VIP address (must be a valid IPv4 address)"
    echo "  --token   Token string (default: 'harvester')"
    echo "  -h, --help  Display this help message"
}

# Function to validate IPv4 address
validate_ip() {
    local ip=$1
    local valid=$(echo $ip | awk -F'.' '$1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255')
    if [[ -z $valid ]]; then
        return 1
    else
        return 0
    fi
}

# Check if script is run as root
if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

# Parse input arguments
ROLE=""
VIP=""
TOKEN="harvester"

while [[ "$#" -gt 0 ]]; do
    case $1 in
        --role)
            ROLE="$2"
            shift
            ;;
        --vip)
            VIP="$2"
            shift
            ;;
        --token)
            TOKEN="$2"
            shift
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            echo "Unknown parameter passed: $1"
            show_help
            exit 1
            ;;
    esac
    shift
done

# Validate input arguments
if [[ -z "$ROLE" ]]; then
    echo "Error: --role is required"
    show_help
    exit 1
fi

if [[ "$ROLE" != "cluster-init" && "$ROLE" != "agent" ]]; then
    echo "Error: --role must be either 'cluster-init' or 'agent'"
    show_help
    exit 1
fi

if [[ -z "$VIP" ]]; then
    echo "Error: --vip is required"
    show_help
    exit 1
fi

if ! validate_ip "$VIP"; then
    echo "Error: --vip must be a valid IPv4 address"
    show_help
    exit 1
fi

# Print parameters
echo "Role: $ROLE"
echo "VIP: $VIP"
echo "Token: $TOKEN"

# Check if template directory exists
if [[ ! -d "./rancherd_config/template" ]]; then
    echo "Error: Template directory ./rancherd_config/template does not exist"
    exit 1
fi

# Copy template directory to real_config directory
rm -rf ./rancherd_config/real_config
cp -r ./rancherd_config/template ./rancherd_config/real_config

# Replace placeholders in files
find ./rancherd_config/real_config -type f -exec sed -i "s/VIP_ADDRESS/$VIP/g" {} +
find ./rancherd_config/real_config -type f -exec sed -i "s/TOKENTOKEN/$TOKEN/g" {} +
find ./rancherd_config/real_config -type f -exec sed -i "s/ROLEROLE/$ROLE/g" {} +

if [[ "$ROLE" == "cluster-init" ]]; then
    # Delete server-url config entry in rancherd config.yaml
    sed -i '/^server:/d' ./rancherd_config/real_config/rancherd/config.yaml
fi

# Copy config.yaml.d to /usr/share/rancher/rancherd/
mkdir -p /usr/share/rancher/rancherd/
cp -r ./rancherd_config/real_config/config.yaml.d /usr/share/rancher/rancherd/

# Copy directories based on role
mkdir -p /etc/rancher
cp -r ./rancherd_config/real_config/rancherd /etc/rancher/
cp -r ./rancherd_config/real_config/rke2 /etc/rancher/

# If the architecture is not arm64, then delete the arm64 multus patch
if [[ "${HOSTTYPE}" != "aarch64" && "$arch" != "arm64" ]]; then
    rm -r /etc/rancher/rancherd/config.yaml.d/23-multus-config.yaml
fi

if [[ "$ROLE" == "agent" ]]; then
    # ADD server config
    rm -rf /etc/rancher/rancherd/config.yaml.d
    rm -f /etc/rancher/rke2/config.yaml.d/90-harvester-server.yaml
fi

echo "Rancherd Configuration preparation complete."



